# Contributor: omni <omni+alpine@hack.org>
# Maintainer: omni <omni+alpine@hack.org>
pkgname=py3-psycopg
pkgver=3.1.15
pkgrel=1
pkgdesc="PostgreSQL adapter for python"
options="!check" # Requires running database
url="https://www.psycopg.org/"
arch="all"
license="LGPL-2.0-or-later"
makedepends="
	cython
	libpq-dev
	py3-gpep517
	py3-setuptools
	py3-tomli
	py3-wheel
	python3-dev
	"
subpackages="
	$pkgname-c-pyc:_c_pyc
	$pkgname-pyc
	$pkgname-c:_c
	"
source="https://github.com/psycopg/psycopg/archive/$pkgver/psycopg-$pkgver.tar.gz
	fix-missing-type-imports.patch
	typing-ext.patch
	psycopg-binary.patch
	"
builddir="$srcdir/psycopg-$pkgver"

prepare() {
	default_prepare

	# Check if we need to update typing-ext.patch
	if grep -r typing_extensions $builddir; then return 1; fi
}

build() {
	cd psycopg
	gpep517 build-wheel \
		--wheel-dir .dist \
		--output-fd 3 3>&1 >&2

	cd ../psycopg_c
	gpep517 build-wheel \
		--wheel-dir .dist \
		--output-fd 3 3>&1 >&2
}

check() {
	make -C psycopg check
	make -C psycopg_c check
}

package() {
	cd psycopg
	python3 -m installer -d "$pkgdir" .dist/*.whl

	cd ../psycopg_c
	python3 -m installer -d "$pkgdir" .dist/*.whl
}

_c() {
	pkgdesc="$pkgdesc - C optimisation distribution"
	depends="$pkgname=$pkgver-r$pkgrel"

	amove usr/lib/python*/site-packages/psycopg_c*
}

_c_pyc() {
	pkgdesc="Precompiled Python bytecode for ${subpkgname%-pyc}"
	install_if="${subpkgname%-pyc}=$pkgver-r$pkgrel pyc"

	cd "$pkgdir"
	# shellcheck disable=SC3003
	local IFS=$'\n'
	# shellcheck disable=SC2046
	amove $(find usr/lib/python*/site-packages/psycopg_c -type d -name __pycache__)
}

sha512sums="
655812616d7ff2d5338f719016db1500496f8e4eeb4559ab10b109e7c1331e5d0a814205217845caed16dc19e68f58ac5e3a4d6d2138b5db6bf079c396644c5d  psycopg-3.1.15.tar.gz
d61e8d6a32ad43e439e906738d1046cf839a9b6576c339b342c46be3565b8863fa0a13205c98f24e8378a8b81939f8eb7465f290264ffd736736508dcb3e2331  fix-missing-type-imports.patch
c3505e8be08da2ab708826fdc5444838348b786009aa61db2032cf8a0a0c42dc47762cc141cebde90582e30c5f0eb6535b1b9fd258bad45d2f10ebd0d3fb0f29  typing-ext.patch
630ae96920d23826db387dc65f0af3f79aff2af933e207db717061d6acfffa220e2e4e9770a663319c0e25974d5434908b7c6340d63a59ce1471dbd182a36263  psycopg-binary.patch
"
